package com.pactera.madp.cp.controller.oms.roomqc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.roomqc.QTRuleDto;
import com.pactera.madp.cp.api.dto.roomqc.RuleTouchDto;
import com.pactera.madp.cp.api.entity.roomqc.ChildRuleEntity;
import com.pactera.madp.cp.api.entity.roomqc.QualityTestingRuleEntity;
import com.pactera.madp.cp.api.entity.roomqc.RuleTouchEntity;
import com.pactera.madp.cp.service.ChildRuleService;
import com.pactera.madp.cp.service.QualityTestingRuleService;
import com.pactera.madp.cp.service.RuleTouchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/qualitytest")
@Api(tags = "质检规则")
public class WorkQTRuleController {

    @Autowired
    private QualityTestingRuleService qualityTestingRuleService;
    @Autowired
    private ChildRuleService childRuleService;
    @Autowired
    private RuleTouchService ruleTouchService;

    @SysLog("质检规则保存")
    @PostMapping
    @ApiOperation(value = "质检规则保存")
    public R save(@RequestBody QTRuleDto dto) {
        Boolean save = qualityTestingRuleService.save(dto);
        return save? R.ok() : R.failed();
    }

    @SysLog("质检规则更新")
    @PutMapping
    @ApiOperation(value = "质检规则更新")
    public R update(@RequestBody QTRuleDto dto) {
        Boolean update = qualityTestingRuleService.update(dto);
        return update? R.ok() : R.failed();
    }

    @SysLog("开启关闭群聊质检状态")
    @GetMapping("/changeStatus")
    @ApiOperation(value = "开启关闭群聊质检状态")
    public R update(Integer ruleId) {
        QualityTestingRuleEntity entity = qualityTestingRuleService.getById(ruleId);
        Boolean isOpen = entity.getIsOpen();
        if (isOpen) {
            entity.setIsOpen(false);
        } else {
            entity.setIsOpen(true);
        }
        boolean b = qualityTestingRuleService.updateById(entity);
        return b? R.ok() : R.failed();
    }

    @SysLog("质检规则删除")
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping
    @ApiOperation(value = "质检规则删除")
    public R delete(Integer id) {
        Boolean remove;
        List<Integer> ids = childRuleService.listObjs(Wrappers.<ChildRuleEntity>lambdaQuery().select(ChildRuleEntity::getId).eq(ChildRuleEntity::getFid, id), o -> Integer.parseInt(o.toString()));
        if (ids.size() != 0 && ids.contains(id)) {
            childRuleService.removeByIds(ids);
            remove = ruleTouchService.remove(Wrappers.<RuleTouchEntity>lambdaQuery().in(RuleTouchEntity::getRuleId, ids));
        }else {
            remove = qualityTestingRuleService.removeById(id);
        }
        return remove ? R.ok() : R.failed();
    }

    @GetMapping
    @ApiOperation(value = "质检规则查询")
    public R query(Page page, RuleTouchDto dto) {
        IPage iPage = qualityTestingRuleService.query(page, dto);
        return R.ok(iPage);
    }

}
